home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter10 / TargetViewpoint1.java < prev    next >
Text File  |  2000-07-11  |  2KB  |  89 lines

  1.  
  2. package custom_nodes;
  3.  
  4. import  shout3d.core.*;
  5. import  shout3d.math.*;
  6.  
  7.  
  8. public class TargetViewpoint1 extends Viewpoint implements FieldObserver {
  9.  
  10.    
  11.    final public FloatField heading = new FloatField(this, "heading",  Field.ANY, 0);
  12.    final public FloatField pitch = new FloatField(this, "pitch", Field.ANY, 0);
  13.    final public FloatField distance = new FloatField(this, "distance", Field.NON_NEGATIVE_FLOAT, 10);   
  14.  
  15.  
  16.    Quaternion q = new Quaternion();                      
  17.    float[] eulers = {0.0f, 0.0f, 0.0f};
  18.  
  19.    //default constructor
  20.    public TargetViewpoint1(){
  21.       this(null);
  22.    }
  23.    
  24.    //constructor that takes
  25.    //a viewer argument
  26.    public TargetViewpoint1(Shout3DViewer viewer){
  27.  
  28.       //call Viewpoint's constructor
  29.       super(viewer);
  30.       
  31.       //register as FieldObserver
  32.       heading.addFieldObserver(this, null);
  33.       pitch.addFieldObserver(this, null);
  34.       distance.addFieldObserver(this, null);
  35.       
  36.       //compute position and oreintation
  37.       setViewpoint(); 
  38.    }
  39.  
  40.    
  41.    protected void finalize() throws Throwable { 
  42.  
  43.       heading.removeFieldObserver(this);
  44.       pitch.removeFieldObserver(this);
  45.       distance.removeFieldObserver(this);
  46.  
  47.       super.finalize();
  48.    }
  49.    
  50.    
  51.    public void onFieldChange(Field theField, Object userData) {
  52.  
  53.       //if TargetViewpoint fields have changed
  54.       if ( theField == heading || theField == pitch || theField == distance) {
  55.          setViewpoint();
  56.       }
  57.       //if something else has changed
  58.       else {
  59.          // call Viewpoint's onFieldChange()
  60.          super.onFieldChange(theField, userData);
  61.       }
  62.    }
  63.    
  64.  
  65.    public void setViewpoint() {
  66.    
  67.       //set quaternion to current heading and pitch
  68.       eulers[0] = heading.getValue();
  69.       eulers[1] = pitch.getValue();
  70.       q.setEulers(eulers);
  71.  
  72.       //set distance vector
  73.       //and rotate it
  74.       if ( distance.getValue() < 0.0f) {
  75.          distance.setValue(0.0f);
  76.       }
  77.       float[] vector = {0.0f, 0.0f, distance.getValue()};
  78.       q.xform(vector);
  79.  
  80.       //set Viewpoint rotation and
  81.       //translate to end of vector
  82.       float[] axisAngle = new float[4];
  83.       q.getAxisAngle(axisAngle);
  84.       orientation.setValue(axisAngle);
  85.       position.setValue(vector);
  86.  
  87.    }
  88. }
  89.